﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
namespace Home.Utils
{
    /// <summary>
    /// Summary description for KTEmail
    /// </summary>
    public class KTEmail
    {
        private static string server;
        private static int port;
        private static string accountId;
        private static string password;
        private static string name;
        private List<string> recipients;
        public KTEmail(string server, int port, string uid, string password, string name)
        {
            recipients = new List<string>();
            Config(server, port, uid, password, name);
        }
        public KTEmail()
        {
            recipients = new List<string>();
        }

        /// <summary>
        /// Configure email settings
        /// </summary>
        ///// <param name="application"></param>
        //public static void Config(Shop.MvcApplication appContext)
        //{
        //    HttpApplicationState application = appContext.Application;
        //    KTEmail.server = application[Shop.Models.SettingModel.SERVER] != null ? application[Shop.Models.SettingModel.SERVER].ToString() : "";
        //    KTEmail.port = Home.Utils.TypeWrapper.GetInt32(application[Shop.Models.SettingModel.PORT] != null ? application[Shop.Models.SettingModel.PORT].ToString() : "0");
        //    KTEmail.accountId = application[Shop.Models.SettingModel.UID] != null ? application[Shop.Models.SettingModel.UID].ToString() : "";
        //    KTEmail.password = application[Shop.Models.SettingModel.PASSWORD] != null ? application[Shop.Models.SettingModel.PASSWORD].ToString() : "";
        //    KTEmail.name = application[Shop.Models.SettingModel.NAME] != null ? application[Shop.Models.SettingModel.NAME].ToString() : "";
        //}
        public static void Config(string server, int port, string uid, string password, string name)
        {

            KTEmail.server = server;
            KTEmail.port = port;
            KTEmail.accountId = uid;
            KTEmail.password = password;
            KTEmail.name = name;
        }

        #region properties
        public static string MailServer { get { return server; } }
        public static int Port { get { return port; } }
        /// <summary>
        /// Full email address
        /// </summary>
        public static string AccountID { get { return accountId; } }
        public static string AccountPassword { get { return password; } }
        public static string AccountDisplayName { get { return name; } }
        public List<string> Reciepients { get { return recipients; } }
        #endregion

        public void AddRecipient(string email) { recipients.Add(email); }
        /// <summary>
        /// Method to send email throung google mail account (smtp)
        /// </summary>       
        /// <param name="to">receiver email</param>
        /// <param name="subject">subject of mail</param>
        /// <param name="body">content of mail</param>
        public void SendMail(string subject, string body)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();
                mailMsg.From = new MailAddress(AccountID, AccountDisplayName);
                foreach (string email in recipients)
                {
                    mailMsg.To.Add(email);
                }
                mailMsg.Subject = subject;
                mailMsg.IsBodyHtml = true;
                mailMsg.Body = body;
                mailMsg.Priority = MailPriority.Normal;
                SmtpClient client = new SmtpClient(MailServer, Port);
                NetworkCredential info = new NetworkCredential(AccountID, AccountPassword);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = info;
                client.EnableSsl = false;
                client.Send(mailMsg);
                System.Threading.Thread.Sleep(3000);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void SendMailAsync(string subject, string body)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();
                //Guid guid = Guid.NewGuid();
                mailMsg.From = new MailAddress(AccountID, AccountDisplayName);
                foreach (string email in recipients)
                {
                    mailMsg.To.Add(email);
                }
                mailMsg.Subject = subject;
                mailMsg.IsBodyHtml = true;
                mailMsg.Body = body;
                mailMsg.Priority = MailPriority.Normal;
                SmtpClient client = new SmtpClient(MailServer, Port);
                NetworkCredential info = new NetworkCredential(AccountID, AccountPassword);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = info;
                client.EnableSsl = false;
                client.SendAsync(mailMsg,null);
                System.Threading.Thread.Sleep(3000);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}

